﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Task_Network_Center.General;
using System.Data;
using System.Windows.Forms;
using System.Windows.Threading;
using Task_Network_Center.Reports;



namespace Task_Network_Center
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : System.Windows.Controls.UserControl
    {
        static ReportBuilder Breport = new ReportBuilder();
        static GCGDataBaseTableLib.TaskTable TTask = new GCGDataBaseTableLib.TaskTable();
        static GCGDataBaseTableLib.DepartementTable TDepartement = new GCGDataBaseTableLib.DepartementTable();
        static GCGDataBaseTableLib.PrioriteTable TPriorite = new GCGDataBaseTableLib.PrioriteTable();
        static GCGDataBaseTableLib.EmployeTable TEmploye = new GCGDataBaseTableLib.EmployeTable();
        static GCGDataBaseTableLib.EquipeTable TEquipe = new GCGDataBaseTableLib.EquipeTable();
        static SQLBuilder Builder = new SQLBuilder();

        object[,] DepartementArray = TDepartement.ListDepartementToStringArray();
        object[,] EmployeArray = TEmploye.ListEmployeToStringArray();
        object[,] PrioriteArray = TPriorite.ListPrioriteToStringArray();
        object[,] EquipeArray = TEquipe.ListEquipeToStringArray();

        DataView ReportTable = null;


        public MainWindow()
        {
            InitializeComponent();
            
           
        }

        private void cmdActualiser_Click(object sender, RoutedEventArgs e)
        {
            updateFilter();
        }

        private void FrmMainTask_Loaded(object sender, RoutedEventArgs e)
        {


            Task_Network_Center.MainDataSet mainDataSet = ((Task_Network_Center.MainDataSet)(this.FindResource("mainDataSet")));
            tbTaches.DataContext = null;
            updateCbo(true);
            datePickerInscription.Text = DateTime.Today.ToShortDateString();

            if (GCGGeneralLib.GeneralVar.StartUp)
            {
                //this.WindowState = System.Windows.WindowState.Minimized;
            }
            //updateFilter();
        }

        private void updateCbo(bool formLoad)
        {
            string[] ProjectList = TTask.ProjectListArray();

            int selectedProject = cboProjets.SelectedIndex;
            int selectedInscritPar = cboInscritPar.SelectedIndex;
            int selectedExecutant = cboExecutant.SelectedIndex;
            int selectedEquipe = cboEquipe.SelectedIndex;
            int selectedPriorite =cboPriorite.SelectedIndex;
            int selectedDepartement = cboDepartement.SelectedIndex;
            int selectedCompletePar = cboComplete.SelectedIndex;

            cboProjets.Items.Clear();
            cboInscritPar.Items.Clear();
            cboExecutant.Items.Clear();
            cboEquipe.Items.Clear();
            cboPriorite.Items.Clear();
            cboDepartement.Items.Clear();
            cboComplete.Items.Clear();


            cboProjets.Items.Add("*");
            cboInscritPar.Items.Add("*");
            cboExecutant.Items.Add("*");
            cboEquipe.Items.Add("*");
            cboPriorite.Items.Add("*");
            cboDepartement.Items.Add("*");
            cboComplete.Items.Add("*");

            for (int i = 0; i < ProjectList.GetLength(0); i++)
            {
                cboProjets.Items.Add(ProjectList[i].ToString());
            }

            for (int i = 0; i < EmployeArray.GetLength(1);i++)
            {

                cboInscritPar.Items.Add(EmployeArray[1, i].ToString());
                    cboExecutant.Items.Add(EmployeArray[1, i].ToString());
                    cboComplete.Items.Add(EmployeArray[1, i].ToString());
                            
            }

            for (int i = 0; i < EquipeArray.GetLength(1); i++)
            {
                cboEquipe.Items.Add(EquipeArray[1, i].ToString());
            }

            for (int i = 0; i < DepartementArray.GetLength(1); i++)
            {
                cboDepartement.Items.Add(DepartementArray[1, i].ToString());
            }

            for (int i = 0; i < PrioriteArray.GetLength(1); i++)
            {
                cboPriorite.Items.Add(PrioriteArray[1, i].ToString());
            }

            if (!formLoad)
            {
                cboProjets.SelectedIndex = selectedProject;
                cboInscritPar.SelectedIndex = selectedInscritPar;
                cboExecutant.SelectedIndex = selectedExecutant;
                cboEquipe.SelectedIndex = selectedEquipe;
                cboPriorite.SelectedIndex = selectedPriorite;
                cboDepartement.SelectedIndex = selectedDepartement;
                cboComplete.SelectedIndex = selectedCompletePar;
            }
            else
            {
            cboProjets.SelectedIndex = 0;
            cboInscritPar.SelectedIndex = 0;
            cboExecutant.SelectedIndex = 0;
            cboEquipe.SelectedIndex = 0;
            cboPriorite.SelectedIndex = 0;
            cboDepartement.SelectedIndex = 0;
            cboComplete.SelectedIndex = 0;
            }

        }

        private void updateFilter()
        {
            
            if ((cboProjets.Text == "*") && (txtDescription.Text == "") && (cboInscritPar.Text == "*") && (datePickerInscription.IsEnabled == false) && (txtSolution.Text == "") &&
                (cboExecutant.Text == "*")  && (txtAvancement.Text == "") && (cboEquipe.Text == "*") && (cboPriorite.Text == "*") && (cboCompleteLe.Text == "*")
                && (cboDepartement.Text == "*") && (cboComplete.Text == "*") && (txtNote1.Text == "") && (txtNote2.Text == "") && (txtNote3.Text == "")
                && (txtNote4.Text == "") && (cboEcheance.Text == "*"))
            {
                //tbTaches.DataContext = TTask.QueryTableReadable("SELECT * FROM tablelistdestaches_general", DepartementArray, EmployeArray, EquipeArray, PrioriteArray).DefaultView;
                
            }
            else
            {
                String query = Builder.BuildFilter(cboProjets, txtDescription, cboInscritPar, datePickerInscription,
            txtSolution, cboExecutant, txtAvancement, cboEquipe, cboPriorite, cboEcheance.SelectedIndex,
            cboDepartement, cboCompleteLe.SelectedIndex, cboComplete, txtNote1, txtNote2, txtNote3, txtNote4,
            DepartementArray, EmployeArray, EquipeArray, PrioriteArray);

                tbTaches.DataContext = TTask.QueryTableReadable(query, DepartementArray, EmployeArray, EquipeArray, PrioriteArray).DefaultView;
                ReportTable = (DataView)tbTaches.DataContext;
                txtDureeTotal.Text = GCGGeneralLib.GeneralVar.DureeTotal.ToString("###.##");
                txtTempsRestant.Text = GCGGeneralLib.GeneralVar.TempsRestantTotal.ToString("###.##");
                if (GCGGeneralLib.GeneralVar.DureeTotal != 0)
                {
                    decimal temp = (GCGGeneralLib.GeneralVar.TempsRestantTotal * 100 / GCGGeneralLib.GeneralVar.DureeTotal);
                    txtCentRestant.Text = temp.ToString("##.##"); 
                }
                else
                {
                    txtCentRestant.Text = "0";
                }
                
            }
            
        }

        private void cboProjets_DropDownClosed(object sender, EventArgs e)
        {
            updateFilter();
        }

        private void cboInscritPar_DropDownClosed(object sender, EventArgs e)
        {
            updateFilter();
        }

        private void cboExecutant_DropDownClosed(object sender, EventArgs e)
        {
            updateFilter();
        }

        private void cboEquipe_DropDownClosed(object sender, EventArgs e)
        {
            updateFilter();
        }

        private void cboPriorite_DropDownClosed(object sender, EventArgs e)
        {
            updateFilter();
        }

        private void cboEcheance_DropDownClosed(object sender, EventArgs e)
        {
            updateFilter();
        }

        private void cboDepartement_DropDownClosed(object sender, EventArgs e)
        {
            updateFilter();
        }

        private void cboCompleteLe_DropDownClosed(object sender, EventArgs e)
        {
            updateFilter();
        }

        private void cboComplete_DropDownClosed(object sender, EventArgs e)
        {
            updateFilter();
        }

        private void txtDescription_TextChanged(object sender, TextChangedEventArgs e)
        {
            updateFilter();
        }

        private void txtSolution_TextChanged(object sender, TextChangedEventArgs e)
        {
            updateFilter();
        }

        private void txtDuree_TextChanged(object sender, TextChangedEventArgs e)
        {
            updateFilter();
        }

        private void txtAvancement_TextChanged(object sender, TextChangedEventArgs e)
        {
            updateFilter();
        }

        private void txtNote1_TextChanged(object sender, TextChangedEventArgs e)
        {
            updateFilter();
        }

        private void txtNote2_TextChanged(object sender, TextChangedEventArgs e)
        {
            updateFilter();
        }

        private void txtNote3_TextChanged(object sender, TextChangedEventArgs e)
        {
            updateFilter();
        }

        private void txtNote4_TextChanged(object sender, TextChangedEventArgs e)
        {
            updateFilter();
        }

        private void chkInscription_Checked(object sender, RoutedEventArgs e)
        {

            datePickerInscription.IsEnabled = true;


        }

        private void chkInscription_Unchecked(object sender, RoutedEventArgs e)
        {
            datePickerInscription.IsEnabled = false;
        }

        private void datePickerInscription_CalendarClosed(object sender, RoutedEventArgs e)
        {
            updateFilter();
        }

        private void cmdAdd_Click(object sender, RoutedEventArgs e)
        {
            FrmAddTask add = new FrmAddTask();
            add.Loaded += new RoutedEventHandler(add_load);
            add.Closed += new EventHandler(add_closed);
            if (cboProjets.Text != "*")
            {
                add.CurrentProject = cboProjets.Text;
            }
            else
	        {
                add.CurrentProject = "";
	        }

            add.TempDepartementArray = DepartementArray;
            add.TempEmployeArray = EmployeArray;
            add.TempEquipeArray = EquipeArray;
            add.TempPrioriteArray = PrioriteArray;
            add.Show();
        }



        private void cmdModif_Click(object sender, RoutedEventArgs e)
        {
            if (tbTaches.SelectedItem != null)
            {
                FrmModifTask modif = new FrmModifTask();
                modif.Loaded += new RoutedEventHandler(modif_load);
                modif.Closed += new EventHandler(modif_closed);
                modif.TempDepartementArray = DepartementArray;
                modif.TempEmployeArray = EmployeArray;
                modif.TempEquipeArray = EquipeArray;
                modif.TempPrioriteArray = PrioriteArray;
                modif.Show();
            }
            else
            {
                System.Windows.MessageBox.Show("Vous devez choisir une ligne...");
            }

        }
        void modif_load(object sender, RoutedEventArgs e)
        {
            this.IsEnabled = false;
        }

        void modif_closed(object sender, EventArgs e)
        {
            this.IsEnabled = true;
            updateCbo(false);
            updateFilter();
        }


        void add_load(object sender, RoutedEventArgs e)
        {
            this.IsEnabled = false;
        }

        void add_closed(object sender, EventArgs e)
        {
            this.IsEnabled = true;
            updateCbo(false);
            updateFilter();
        }

        private void tbTaches_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            DataRowView drv = (DataRowView)tbTaches.SelectedItem;
            if (drv != null)
            {
                GCGGeneralLib.GeneralVar.CurrentSelectedCell = Convert.ToInt32(drv.Row.ItemArray[0].ToString());
            }
        }

        private void tbTaches_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (Key.Delete == e.Key)
            {
                if (tbTaches.SelectedItem != null)
                {
                    MessageBoxResult resultat = System.Windows.MessageBox.Show("Voulez-vous vraiment effacer cette sélection?", "ATTENTION", MessageBoxButton.YesNo);
                    if (resultat == MessageBoxResult.Yes)
                    {
                        TTask.DeleteRow(GCGGeneralLib.GeneralVar.CurrentSelectedCell);
                        updateFilter();
                        updateCbo(false);
                    }

                }
                else
                {
                    System.Windows.MessageBox.Show("Vous devez choisir une ligne...");
                }

            }
        }



        

        private void FrmMainTask_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {

        }

        private void tbTaches_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (tbTaches.SelectedItem != null)
            {
                FrmModifTask modif = new FrmModifTask();
                modif.Loaded += new RoutedEventHandler(modif_load);
                modif.Closed += new EventHandler(modif_closed);
                modif.TempDepartementArray = DepartementArray;
                modif.TempEmployeArray = EmployeArray;
                modif.TempEquipeArray = EquipeArray;
                modif.TempPrioriteArray = PrioriteArray;
                modif.Show(); 
            }

        }



        private void cmdPrint_Click(object sender, RoutedEventArgs e)
        {
            if (tbTaches.Items.Count != 0)
            {
                FrmTaskReport report = new FrmTaskReport();
                Breport.PrepareTable(ReportTable);
                report.Show(); 
            }
            else
            {
                System.Windows.MessageBox.Show("Aucun item dans la grille à imprimer...");
            }

        }
		
		void userControl1_Initialized(object sender, EventArgs e)
		{
        

            Task_Network_Center.MainDataSet mainDataSet = ((Task_Network_Center.MainDataSet)(this.FindResource("mainDataSet")));
            tbTaches.DataContext = null;
            updateCbo(true);
            datePickerInscription.Text = DateTime.Today.ToShortDateString();

            if (GCGGeneralLib.GeneralVar.StartUp)
            {
                //this.WindowState = System.Windows.WindowState.Minimized;
            }
            //updateFilter();			
		}

        private void cmdCopy_Click(object sender, RoutedEventArgs e)
        {
            if (tbTaches.SelectedItem != null)
            {
                FrmCopyTask copy = new FrmCopyTask();
                copy.Loaded += new RoutedEventHandler(copy_load);
                copy.Closed += new EventHandler(copy_closed);
                copy.TempDepartementArray = DepartementArray;
                copy.TempEmployeArray = EmployeArray;
                copy.TempEquipeArray = EquipeArray;
                copy.TempPrioriteArray = PrioriteArray;
                copy.Show();
            }
            else
            {
                System.Windows.MessageBox.Show("Vous devez choisir une ligne...");
            }
        }

        void copy_load(object sender, RoutedEventArgs e)
        {
            this.IsEnabled = false;
        }

        void copy_closed(object sender, EventArgs e)
        {
            this.IsEnabled = true;
            updateCbo(false);
            updateFilter();
        }
    }
}
